<?php

class FormUserRecoveryCommit extends CFormModel
{
	public $email;
	public $secret;
	public $password;
	public $verifyPassword;
	
	public function rules() {
		return array(
			array('email', 'required'),
			array('email', 'email'),
			
			array('secret', 'required'),
			
			array('password', 'required'),
			array('password', 'length', 'min' => Yii::app()->getModule('user')->settings['password']['minLength'], 'message' => UserModule::t('Invalid password (minimal length: {min} characters)', array('{min}' => Yii::app()->getModule('user')->settings['password']['minLength']))),
			
			array('verifyPassword', 'required'),
			array('verifyPassword', 'compare', 'compareAttribute'=>'password', 'message' => UserModule::t("Passwords do not match.")),
		);
	}

	public function attributeLabels()
	{
		return array(
			'password'       => UserModule::t('Password'),
			'verifyPassword' => UserModule::t('Retype Password'),
		);
	}
	
	public function commit()
	{
		if($this->validate())
		{
			$model = User::model()->notsafe()->findByAttributes(array('email' => $this->email));
			
			if($model)
			{
				return $model->changePassword($this->secret, $this->password);
			}
			return false;
		}
		return false;
	}
	
}